package share.service;

import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import share.dao.entity.GrossRevenueReportDO;
import share.dao.mapper.GrossRevenueReportDOMapper;
import share.service.model.GrossRevenueReportModel;

import java.util.Date;

@Service
public class GrossRevenueReportServiceImpl implements GrossRevenueReportService{

    @Autowired
    private GrossRevenueReportDOMapper grossRevenueReportDOMapper;

    @Override
    public GrossRevenueReportModel addGrossRevenueReport(GrossRevenueReportModel recordModel) {
        GrossRevenueReportDO grrDO = convertFromModel(recordModel);

        grossRevenueReportDOMapper.insertSelective(grrDO);

        recordModel.setId(grrDO.getId());
        return recordModel;
    }

    @Override
    public GrossRevenueReportModel selectByShareId(Integer shareId) {
        GrossRevenueReportDO grrDO = grossRevenueReportDOMapper.selectByShareId(shareId);
        GrossRevenueReportModel recordModel = convertFromDO(grrDO);
        return recordModel;
    }

    private GrossRevenueReportModel convertFromDO(GrossRevenueReportDO grrDO) {
        if (grrDO == null) {
            return null;
        }
        GrossRevenueReportModel grrModel = new GrossRevenueReportModel();
        BeanUtils.copyProperties(grrDO, grrModel);
        grrModel.setRevenueReportDate1(fromDateToStr(grrDO.getRevenueReportDate1()));
        grrModel.setRevenueReportDate2(fromDateToStr(grrDO.getRevenueReportDate2()));
        grrModel.setRevenueReportDate3(fromDateToStr(grrDO.getRevenueReportDate3()));
        grrModel.setRevenueReportDate4(fromDateToStr(grrDO.getRevenueReportDate4()));
        grrModel.setRevenueReportDate5(fromDateToStr(grrDO.getRevenueReportDate5()));
        grrModel.setRevenueReportDate6(fromDateToStr(grrDO.getRevenueReportDate6()));
        grrModel.setRevenueReportDate7(fromDateToStr(grrDO.getRevenueReportDate7()));
        grrModel.setRevenueReportDate8(fromDateToStr(grrDO.getRevenueReportDate8()));
        grrModel.setRevenueReportDate9(fromDateToStr(grrDO.getRevenueReportDate9()));
        return grrModel;
    }

    private GrossRevenueReportDO convertFromModel(GrossRevenueReportModel recordModel) {
        GrossRevenueReportDO grrDO = new GrossRevenueReportDO();
        BeanUtils.copyProperties(recordModel, grrDO);
        grrDO.setRevenueReportDate1(formatDate(recordModel.getRevenueReportDate1()));
        grrDO.setRevenueReportDate2(formatDate(recordModel.getRevenueReportDate2()));
        grrDO.setRevenueReportDate3(formatDate(recordModel.getRevenueReportDate3()));
        grrDO.setRevenueReportDate4(formatDate(recordModel.getRevenueReportDate4()));
        grrDO.setRevenueReportDate5(formatDate(recordModel.getRevenueReportDate5()));
        grrDO.setRevenueReportDate6(formatDate(recordModel.getRevenueReportDate6()));
        grrDO.setRevenueReportDate7(formatDate(recordModel.getRevenueReportDate7()));
        grrDO.setRevenueReportDate8(formatDate(recordModel.getRevenueReportDate8()));
        grrDO.setRevenueReportDate9(formatDate(recordModel.getRevenueReportDate9()));
        grrDO.setCreateDate(new Date());
        return grrDO;
    }

    private Date formatDate(String source) {
        return new DateTime(source).toDate();
    }

    private String fromDateToStr(Date source) {
        return new DateTime(source).toString("yy-MM-dd");
    }

}
